package com.zchat.android.app;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.preference.PreferenceManager;

import com.zchat.android.R;
import com.zchat.android.handler.exception.ApplicationCrashHandler;
import com.zchat.api.ZChat;
import com.zchat.api.log.Logger;
import com.zchat.api.util.ZChatSetting;

/**
 * 
 * @author Oliverzhu
 *
 */
public abstract class BaseActivity extends Activity {
	protected static final boolean DEBUG = ZChatSetting.DEBUG;
	
	protected Context context;
	
	protected Handler uiHandler = new MainThreadHandler(Looper.getMainLooper());
	
	/** 消息标志 */
	public static final int SHOWPROGRESSDIALOG = 1;
	
	public static final int CLOSEPROGRESSDIALOG = 2;
	
	protected ZChat zchat;
	
	protected SharedPreferences mPrefs;
	
	private ProgressDialog mProgressDialog;
	/**
	 * 主线程Handler对象
	 * @author zjw
	 *
	 */
	class MainThreadHandler extends Handler
	{
		public MainThreadHandler(Looper looper)
		{
			super(looper);
		}
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what)
			{
				case SHOWPROGRESSDIALOG:
					showProgressDialog();
					break;
				case CLOSEPROGRESSDIALOG:
					dismissProgressDialog();
					break;
					
			}
			
			super.handleMessage(msg);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//注册异常处理机
		Thread.setDefaultUncaughtExceptionHandler(
        		new ApplicationCrashHandler(this));
		
		context = this;
		
		//获取聊天工具
		LauncherApplication launcherApplication = (LauncherApplication)getApplication();
		
		zchat = launcherApplication.getZChat(context);
		
		//获取配置文件工具
		mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		zchat.getCache().put(toString(), context);
		
		initComponents();
		
		setComponentListeners();
		
		initDatas();
		
	}
	
	public Handler getMainThreadHandler()
	{
		return uiHandler;
	}
	
	private ProgressDialog showProgressDialog() {
        if (mProgressDialog == null) {
            ProgressDialog dialog = new ProgressDialog(this);
            dialog.setTitle(R.string.tip);
            dialog.setMessage(getString(R.string.loading));
            dialog.setIndeterminate(true);
            dialog.setCancelable(true);
            mProgressDialog = dialog;
        }
        mProgressDialog.show();
        return mProgressDialog;
    }

    private void dismissProgressDialog() {
        try {
        	if(mProgressDialog != null && mProgressDialog.isShowing())
        	{
        		mProgressDialog.dismiss();
        	}
        } catch (IllegalArgumentException e) {
            // We don't mind. android cleared it for us.
        }
    }
	
	/**
	 * 初始化当前Activity所要展示的组件
	 */
	protected abstract void initComponents();
	
	/**
	 * 给当前组件赋值，比如设置adapter,启动异步任务
	 */
	protected abstract void initDatas();
	
	/**
	 * 给当前组件设置监听
	 */
	protected abstract void setComponentListeners();
	
	@Override
	protected void onResume() {
		super.onResume();
		if(DEBUG) Logger.debug(toString()+"-onResume()");
	}

}
